#include"common.hpp"
using std::cout;
using std::endl;
using std::cerr;


int main()
{
    key_t key=ftok(PATH_NAME,PROJ_ID);
    cout<<"Start share memory!"<<endl;
    sleep(1);
    int shmid=shmget(key,SHM_SIZE,IPC_CREAT);
    if(shmid==-1)
    {
        cerr<<"shmget error!"<<endl;
        exit(2);
    }
    cout<<"shmget success!shmid::"<<shmid<<"  key::"<<key<<endl;

    sleep(2);
    char *str=(char*)shmat(shmid,nullptr,0);
    if(str==(void*)-1)
    {
        cerr<<"shmat error!"<<endl;
        exit(2);
    }
    cout<<"Shmat success!"<<endl;

    int cnt=0;
    while(true)
    {
        str[cnt]='A'+cnt;
        cnt++;
        str[cnt]='\0';
        sleep(1);
    }

    int dtret=shmdt(str);
    if(dtret==-1)
    {
        cerr<<"Dettach fail!"<<endl;
        exit(2);
    }
    cout<<"Dettach success!"<<endl;
    return 0;
}
